package me.jzn.alib.utils;

import android.Manifest.permission;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;

import androidx.annotation.RequiresPermission;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import me.jzn.alib.ALib;


public class AndrMetaUtil {
    private static final Logger log = LoggerFactory.getLogger(AndrMetaUtil.class);

    //制造商
    public static String getManufactuer() {
        return Build.MANUFACTURER;
    }

    //品牌
    public static String getBrand() {
        return Build.BRAND;

    }

    //型号
    public static String getModel() {
        return Build.MODEL;
    }

    //每次刷机都会变
    public static String getAndroidId() {
        return android.provider.Settings.Secure
                .getString(ALib.app().getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
    }

    @RequiresPermission(anyOf = {
            permission.READ_PHONE_STATE,
            permission.READ_SMS,
            permission.READ_PHONE_NUMBERS
    })
    public static String getPhoneNo() {
        if (Build.VERSION.SDK_INT >= 33) {
            SubscriptionManager manager = CtxUtil.getSystemService(SubscriptionManager.class);
            return manager.getPhoneNumber(SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
        }
        TelephonyManager tm = CtxUtil.getSystemService(TelephonyManager.class);
        return tm.getLine1Number();
    }

    //===================================================
    @Deprecated
    @SuppressLint("MissingPermission")
    @RequiresPermission(permission.READ_PHONE_STATE)
    public static String getSN() {
        if (Build.VERSION.SDK_INT >= 29) {
            //throw SecurityException,无法被获取
            return null;
        }
        if (Build.VERSION.SDK_INT >= 26) {
            return Build.getSerial();
        }
        return Build.SERIAL;
    }


    //IMEI=(IMEI for GSM or MEID or ESN for CDMA)只能支持拥有通话功能的设备，对于平板不可以。
    @Deprecated
    @SuppressLint("MissingPermission")
    @RequiresPermission(permission.READ_PHONE_STATE)
    public static String getIMEI() {
        if (Build.VERSION.SDK_INT >= 29) {
            //throw SecurityException,无法被获取
            return null;
        }
        TelephonyManager tm = (TelephonyManager) ALib.app().getSystemService(Context.TELEPHONY_SERVICE);
        if (Build.VERSION.SDK_INT >= 26) {
            return tm.getImei();
        }
        return tm.getDeviceId();
    }

    /**
     * IMSI=MCC[460]+MNC[01]+MSIN[1234567890]<br/>
     * IMSI储存在SIM卡的EF-IMSI文件中，可用于区别移动用户的有效信息。这个MSIN号是运营商在制卡中心制卡时已经唯一写入到卡中，可以区分每一张SIM卡。<br/>
     * <br/>
     * MSISDN=CC[86]+NDC[186]+SN[12345678]<br/>
     * MSISDN储存在SIM卡的EF-MSISDN文件中，<br/>
     * 但是该文件一般可以被更改，甚至可以被删除，所以一般不以该文件存储的内容为准。<br/>
     * 需要知道MSISDN号码，最常用的方法是拨出一个电话，看对方手机显示的是什么号码。<br/>
     * <br/>
     * IMSI是SIM卡的ID，MSISDN是用户的ID。意思就是，当用户去营业厅换SIM卡的时候，IMSI就变了，而MSISDN不变。
     */
    @Deprecated
    @SuppressLint("MissingPermission")
    @RequiresPermission(permission.READ_PHONE_STATE)
    public static String getIMSI() {
        TelephonyManager tm = (TelephonyManager) ALib.app().getSystemService(Context.TELEPHONY_SERVICE);
        return tm.getSubscriberId();
    }


    @Deprecated
    @SuppressLint("MissingPermission")
    @RequiresPermission(permission.READ_PHONE_STATE)
    public static String getICCID() {
        TelephonyManager tm = CtxUtil.getSystemService(TelephonyManager.class);
        return tm.getSimSerialNumber();
    }
}
